package com.xxxx.yebserver.controller;


import com.xxxx.yebserver.common.R;
import com.xxxx.yebserver.entity.Admin;
import com.xxxx.yebserver.pojo.MySecurityUser;
import com.xxxx.yebserver.service.AdminService;
import com.xxxx.yebserver.vo.LoginVo;
import com.xxxx.yebserver.vo.RegistVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cxq
 * @since 2021-06-15
 */
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    AdminService adminService;

    @ApiOperation("登录")
    @PostMapping("login")
    public R  login(@RequestBody LoginVo vo){
        String token = adminService.getTokenByUsername(vo);
        return R.ok().data(token);
    }

    @ApiOperation("获取当前登录的用户信息")
    @GetMapping("info")
    public R  info(){
        MySecurityUser user = (MySecurityUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Admin admin = user.getAdmin();
        admin.setPassword(null);
        return R.ok().data(admin);
    }

    @ApiOperation("注册")
    @PostMapping("regist")
    public R  regist(@RequestBody RegistVo vo){
        if(adminService.insertAdmin(vo)){
            return R.ok();
        }else{
            return R.error();
        }
    }
    //TODO 1.获取所有操作员，2.更新操作员，3更新操作员角色，4.获取所有角色，5.删除操作员

    @ApiOperation("测试")
    @GetMapping("demo")
    public R  demo(){
        return R.ok().msg("测试成功");
    }
}

